/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Install.java
 *
 * Created on Aug 26, 2010, 4:29:10 AM
 */
package GUI_Config;

import DA.*;
import java.io.*;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author thangvv0907i
 */
public class Install extends javax.swing.JFrame {

    /** Creates new form Install */
    public Install(boolean bExamples) {
        initComponents();
        InstallOperation io = new InstallOperation(this, bExamples);
        io.start();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pgrInstall = new javax.swing.JProgressBar();
        lblStatus = new javax.swing.JLabel();
        btnCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lblStatus.setText("Installing...");

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pgrInstall, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addComponent(lblStatus)
                    .addComponent(btnCancel, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 85, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblStatus)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pgrInstall, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCancel)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        int iChoose = JOptionPane.showConfirmDialog(this, "Do you want to exit setup?", "Exit Setup", JOptionPane.YES_NO_OPTION);
        if (iChoose == JOptionPane.YES_OPTION) {
            System.exit(0);
        }
}//GEN-LAST:event_btnCancelActionPerformed
    public void setStatus(String sStatus, int percent) {
        lblStatus.setText((sStatus));
        pgrInstall.setValue(percent);
    }

    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JLabel lblStatus;
    private javax.swing.JProgressBar pgrInstall;
    // End of variables declaration//GEN-END:variables
}

class InstallOperation extends Thread {

    private Install frmParent;
    private boolean bExamples;
    public InstallOperation(Install frmParent, boolean bExamples) {
        this.frmParent = frmParent;
    }

    @Override
    public void run() {
        saveConfig();
        frmParent.setStatus("Saving Configs", 0);

        install("install.txt", "Installing Database...");
        install("examples.txt", "Installing Examples...");
        if (bExamples) {
        }
        JOptionPane.showMessageDialog(frmParent, "Installation is successful.");
        System.exit(0);
    }

    private void saveConfig() {
        ConfigFile.setProperty(ConfigFile.CONFIG_SERVERNAME, ConnectionDB.getServername());
        ConfigFile.setProperty(ConfigFile.CONFIG_PORTNUMBER, ConnectionDB.getPort());
        ConfigFile.setProperty(ConfigFile.CONFIG_SERVERUSERNAME, ConnectionDB.getUsername());
        ConfigFile.setProperty(ConfigFile.CONFIG_SERVERPASSWORD, ConnectionDB.getPassword());
        ConfigFile.setProperty(ConfigFile.CONFIG_DATABASENAME, ConnectionDB.getDatabase());
        ConfigFile.saveConfiguration();
    }

    private void install(String sFileName, String sStatus) {
        FileReader fr = null;
        BufferedReader br = null;
        ArrayList<String> sQueries = new ArrayList<String>();

        try {
            fr = new FileReader(sFileName);
            br = new BufferedReader(fr);
        } catch (Exception ex) {
            ex.printStackTrace();
            System.exit(0);
        }

        try {
            String s = br.readLine();
            while(s != null) {
                sQueries.add(s);
                s = br.readLine();
            }
        } catch(Exception ex) {
        }

        double iDid = 0;
        for(String s : sQueries) {
            ConnectionDB.exec(s);
            ++iDid;
            double dPercent = (iDid / sQueries.size()) * 100;
            frmParent.setStatus(sStatus, (int)dPercent);
        }
        try {
            br.close();
            fr.close();
        } catch (Exception ex) {

        }

    }
}
